/**
* Copyright (c) 2016-2100 yunclouds
*/
package com.yunqian.redot.service;

import com.yunqian.exception.ServiceException;
import com.yunqian.redot.domain.Role;
import com.yunqian.redot.repository.RoleRepository;
import com.yunqian.service.BaseService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @author czy
 * 2018-11-08 09:41:18
 */
@Log4j2
@Service
public class RoleService extends BaseService<Role, Long> {

    @Autowired
    private RoleRepository roleRepository;

    @Override
    public Role save(Role role) {
        boolean exist = roleRepository.existsByNameZh(role.getNameZh());
        if(exist){
            throw new ServiceException("添加失败，该角色名已存在");
        }
        return super.save(role);
    }

    @Override
    public Role update(Long id, Map<String, Object> map) {
        boolean exist = roleRepository.existsByNameZhAndIdNot((String)map.get("nameZh"), id);
        if(exist){
            throw new ServiceException("修改失败，该角色名已存在");
        }
        return super.update(id, map);
    }
}
